import os
import sys
import time
import traceback

sys.path.append("..")

from src import facade
from src.facade import configdicts, urlQuery2List
from src.mylogger import MLogger
from src.mysqlfacade import MysqlUtiles

abspath = os.path.dirname(os.path.abspath(__file__))
configpath = os.path.join(abspath, "db.ini")


class Downjournal(object):
    def __init__(self):
        self.logger = MLogger().streamlogger
        self.listdir = configdicts["path"]["path_step3"]
        facade.create_dir(self.listdir)
        self.mysqlutile = MysqlUtiles(configpath, "db", logger=self.logger)
        self.host = configdicts["other"]["host"]
        self.baseurl = "http://" + self.host + "/Kns55/oldnavi/"

        self.header = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Host": self.host,
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
        }

    def selectdb(self):
        sql = "select url from journal where stat=0 and failcount < 3"
        rows = self.mysqlutile.SelectFromDB(sql)
        if rows:
            for row in rows:
                time.sleep(1)
                url = row[0]
                fullurl = self.baseurl + url
                dicts = urlQuery2List(fullurl)
                BaseID = dicts["BaseID"][0]
                filepath = os.path.join(self.listdir, BaseID + ".html")
                if os.path.exists(filepath):
                    self.logger.info("文件存在:{}".format(filepath))
                    sql = "update journal set stat=1 where url='{}'".format(url)
                    self.mysqlutile.ExeSqlToDB(sql)
                    continue
                if self.down_journal(fullurl, BaseID, filepath):
                    sql = "update journal set stat=1 where url='{}'".format(url)
                    self.mysqlutile.ExeSqlToDB(sql)
                else:
                    sql = "update journal set failcount=failcount+1 where url='{}'".format(url)
                    self.mysqlutile.ExeSqlToDB(sql)
        else:
            return "break"

    def down_journal(self, fullurl, BaseID, filepath):
        BoolResult, errString, r = facade.BaseRequest(fullurl,
                                                      headers=self.header,
                                                      mark="tdInfo",
                                                      timeout=(30, 60))
        if BoolResult:
            if r.text.find(BaseID) > -1:
                try:
                    facade.single_write_file(r.text, filepath)
                except:
                    traceback.print_exc()
                    return False
                return True
            else:
                return False
        else:
            self.logger.info(errString)
            facade.single_write_file(r.text, "./err_step3.html")
            return False


if __name__ == "__main__":
    down = Downjournal()
    for _ in range(3):
        results = down.selectdb()
        if results == "break":
            down.logger.info("break")
            break
